package com.yiyuan.app.config;


import com.alibaba.mqtt.server.ServerProducer;
import com.alibaba.mqtt.server.config.ChannelConfig;
import com.alibaba.mqtt.server.config.ProducerConfig;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

@Slf4j
@Configuration
@AllArgsConstructor
public class MQTTProducerConfig {

    private final MQTTProperties mqttProperties;


    @Bean
    public ServerProducer serverProducer() throws IOException, TimeoutException {
        ChannelConfig channelConfig = new ChannelConfig();
        channelConfig.setDomain(mqttProperties.getDomain());
        channelConfig.setPort(mqttProperties.getPort());
        channelConfig.setInstanceId(mqttProperties.getInstanceId());
        channelConfig.setAccessKey(mqttProperties.getAccessKey());
        channelConfig.setSecretKey(mqttProperties.getSecretKey());

        ServerProducer serverProducer = new ServerProducer(channelConfig, new ProducerConfig());
        serverProducer.start();
        log.info("ServerProducer 服务已开启");
        return serverProducer;
    }

}
